package com.t.bricks.bussiness.service.simpleuser.impl;

import java.util.ArrayList;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.t.bricks.bussiness.db.dao.simpleuser.SimpleUserDao;
import com.t.bricks.bussiness.db.entity.simpleuser.SimpleUser;
import com.t.bricks.bussiness.service.simpleuser.SimpleUserService;
import com.t.bricks.model.MsgEmity;
import com.t.bricks.model.Page;
import com.t.bricks.utils.function.encode.MD5Util;

@Service
public class SimpleUserServiceImpl implements SimpleUserService {

	@Autowired
	private SimpleUserDao simpleUserDao;
	
	/**
	 * 登录请求,验证用户密码是否正确
	 * @param sName
	 * @param sPass
	 * @return
	 */
	@Override
	public MsgEmity login(String sName, String sPass) {
		if (null == sName || "".equals(sName.trim())) {
			return new MsgEmity(false, "登录名不能为空！");
		}
		if (null == sPass || "".equals(sPass.trim())) {
			return new MsgEmity(false, "密码不能为空！");
		}
		
		SimpleUser simpleUserData = new SimpleUser();
		simpleUserData.setsName(sName);
		simpleUserData.setsPass(sPass);
		int iCount = simpleUserDao.findCountByKey(simpleUserData);
		if (iCount < 1) {
			return new MsgEmity(false, "用户或密码错误！", 90);
		}
		
		if (iCount != 1) {
			return new MsgEmity(false, "发现重名用户！", 91);
		}
		
		MsgEmity me = simpleUserDao.findByKey(simpleUserData);
		if (!me.isSuccess()) {
			return new MsgEmity(false, "查询失败，请稍后重试！");
		}
		
		SimpleUser simpleUser = me.getData();
		
		return new MsgEmity(true, "用户密码正确！", simpleUser);
	}
	
	/**
	 * 添加用户
	 * @param simpleUser
	 * @return
	 */
	@Override
	public MsgEmity add(SimpleUser simpleUser) {
		if (null == simpleUser.getsName() || "".equals(simpleUser.getsName().trim())) {
			return new MsgEmity(false, "用户名不能为空！", 91);
		}
		
		if (null == simpleUser.getsPass() || "".equals(simpleUser.getsPass().trim())) {
			return new MsgEmity(false, "密码不能为空！", 92);
		}

		if (simpleUser.getsName().length() > 32) {
			return new MsgEmity(false, "用户名长度超出！只能允许32个英文字符或16个中文字符", 93);
		}

		if (simpleUser.getsPass().indexOf(" ") > -1 || simpleUser.getsPass().indexOf("　") > -1) {
			return new MsgEmity(false, "密码中不能含有空格！", 94);
		}

		if (simpleUser.getsPass().length() > 32) {
			return new MsgEmity(false, "密码长度超出！只能允许32个英文字符或16个中文字符", 95);
		}

		if (simpleUser.getsMemo().length() > 200) {
			return new MsgEmity(false, "备注长度超出！只能允许200个英文字符或100个中文字符", 96);
		}
		
		SimpleUser simpleUserData = new SimpleUser();
		simpleUserData.setsName(simpleUser.getsName().trim());
		if (0 < simpleUserDao.findCountByKey(simpleUserData)) {
			return new MsgEmity(false, "已存在相同名称的用户！", 97);
		}
		
		simpleUser.setsName(simpleUser.getsName().trim());
		simpleUser.setsMemo(simpleUser.getsMemo().trim());
		simpleUser.setsPass(MD5Util.md5(simpleUser.getsPass().trim()));
		simpleUser.setdModifiedDate(new Date());
		
		MsgEmity me = simpleUserDao.add(simpleUser);
		
		if (!me.isSuccess()) {
			return new MsgEmity(false, "新增失败，请稍后重试！", 98);
		}
		
		return new MsgEmity(true, "新增成功！", simpleUser.getiId());
	}
	
	/**
	 * 删除
	 * @param iId
	 * @return
	 */
	@Override
	public MsgEmity del(Integer iId) {
		if (null == iId || iId.intValue() < 1) {
			return new MsgEmity(false, "请求的数据不存在！", 90);
		}
		
		SimpleUser simpleUserData = new SimpleUser();
		simpleUserData.setiId(iId);
		
		if (0 >= simpleUserDao.findCountByKey(simpleUserData)) {
			return new MsgEmity(false, "请求的数据不存在！", 91);
		}
		
		MsgEmity me = simpleUserDao.del(iId);
		if (!me.isSuccess()) {
			return new MsgEmity(false, "删除失败，请稍后重试！", 92);
		}
		
		return new MsgEmity(true, "删除用户成功！", 99);
	}
	
	/**
	 * 修改密码
	 * @param iId
	 * @param sOldPass
	 * @param sNewPass
	 * @return
	 */
	@Override
	public MsgEmity editPass(Integer iId, String sOldPass, String sNewPass) {
		if (null == iId || iId.intValue() < 1) {
			return new MsgEmity(false, "请求的用户编号不能为空！", 88);
		}
		if (null == sOldPass || "".equals(sOldPass.trim())) {
			return new MsgEmity(false, "旧密码不能为空！", 89);
		}
		if (null == sNewPass || "".equals(sNewPass.trim())) {
			return new MsgEmity(false, "新密码不能为空！", 90);
		}
		if (sNewPass.indexOf(" ") > -1 || sNewPass.indexOf("　") > -1) {
			return new MsgEmity(false, "密码中不能含有空格！", 91);
		}
		if (sNewPass.length() > 32) {
			return new MsgEmity(false, "密码长度超出！只能允许32个英文字符或16个中文字符", 92);
		}
		if (sOldPass.equals(sNewPass.trim())) {
			return new MsgEmity(false, "新旧密码一致，没有修改的必要！", 93);
		}
		
		SimpleUser simpleUserData = new SimpleUser();
		simpleUserData.setiId(iId);
		if (0 == simpleUserDao.findCountByKey(simpleUserData)) {
			return new MsgEmity(false, "没有找到用户！", 94);
		}
		
		MsgEmity me = simpleUserDao.findById(iId);
		if (!me.isSuccess() || null == me.getData()) {
			return new MsgEmity(false, "没有找到用户！", 95);
		}
		
		SimpleUser simpleUser = me.getData();
		
		String sPass = MD5Util.md5(sOldPass);
		if (!sPass.equals(simpleUser.getsPass()) ) {
			return new MsgEmity(false, "旧密码错误！", 96);
		}

		String sMd5Pass = MD5Util.md5(sNewPass);
		
		me = simpleUserDao.editPass(iId, sPass, sMd5Pass);
		if (!me.isSuccess()) {
			return new MsgEmity(false, "修改失败，请稍后重试！", 97);
		}
		
		if (me.getData().equals("91")) {
			return new MsgEmity(false, "修改失败！", 98);
		}
		
		return new MsgEmity(true, "修改密码成功！", me.getData());
	}
	
	/**
	 * 查询分页信息
	 * @param sName
	 * @param orders
	 * @param page
	 * @return
	 */
	@Override
	public MsgEmity findByPage(String sName, String orders, Page page) {
		if (null == page) {
			page = new Page();
			page.setRecordList(new ArrayList<SimpleUser>());
		}
		if (page.getPageCurrent() < 1) {
			return new MsgEmity(false, "分页行不能为空且必须大于0！", 91);
		}
		if (page.getPageSize() < 1) {
			return new MsgEmity(false, "每页显示数不能为空且必须大于1！", 92);
		}
		
		return simpleUserDao.findByPage(sName, orders, page);
	}
	
	 /**
	 * 根据Id查询数据
	 * @param iId
	 * @return
	 */
	@Override
	public MsgEmity findById(Integer iId) {
		if (null == iId || iId.intValue() < 1) {
			return new MsgEmity(false, "编号不能为空！", 94);
		}
		
		return simpleUserDao.findById(iId);
	}
	
	/**
	 * 修改用户信息
	 * @param simpleUser
	 * @param sNewPass
	 * @return
	 */
	@Override
	public MsgEmity edit(SimpleUser simpleUser, String sNewPass) {
		if (null == simpleUser) {
			return new MsgEmity(false, "更新失败，请稍后重试！", 90);
		}

		if (null == simpleUser.getsName() || "".equals(simpleUser.getsName().trim())) {
			return new MsgEmity(false, "用户名不能为空！", 91);
		}
		
		if (null == simpleUser.getsPass() || "".equals(simpleUser.getsPass().trim())) {
			return new MsgEmity(false, "密码不能为空！", 92);
		}

		if (simpleUser.getsName().length() > 32) {
			return new MsgEmity(false, "用户名长度超出！只能允许32个英文字符或16个中文字符", 93);
		}

		if (simpleUser.getsPass().indexOf(" ") > -1 || simpleUser.getsPass().indexOf("　") > -1) {
			return new MsgEmity(false, "密码中不能含有空格！", 94);
		}

		if (simpleUser.getsPass().length() > 32) {
			return new MsgEmity(false, "密码长度超出！只能允许32个英文字符或16个中文字符", 95);
		}

		if (simpleUser.getsMemo().length() > 200) {
			return new MsgEmity(false, "备注长度超出！只能允许200个英文字符或100个中文字符", 96);
		}
		
		String sName = simpleUser.getsName().trim();
		String sPass = simpleUser.getsPass().trim();
		
		MsgEmity me = simpleUserDao.findById(simpleUser.getiId());
		SimpleUser simpleUserData = me.getData();
		if (!MD5Util.md5(sPass).equals(simpleUserData.getsPass()) ) {
			return new MsgEmity(false, "旧密码错误！", 97);
		}
		
		//--没有企图修改用户名--//
		if (simpleUserData.getsName().equals(sName)) {
			me = simpleUserDao.edit(simpleUser.getiId(), sName, sNewPass);
			if (!me.isSuccess()) {
				return new MsgEmity(false, me.getMsg());
			}
			
			return new MsgEmity(true, "修改成功！", me.getData());
		}
		
		//--用户名被修改--//
		me = simpleUserDao.findByName(sName);
		if (me.isSuccess()) {
			return new MsgEmity(false, "用户名称已存在！", 97);
		}
		
		me = simpleUserDao.edit(simpleUser.getiId(), sName, sNewPass);
		if (!me.isSuccess()) {
			return new MsgEmity(false, me.getMsg(), 98);
		}
		
		return new MsgEmity(true, "修改成功！", me.getData());
	}

	/**
	 * 根据关键值查询数据
	 * @param simpleUser
	 * @return
	 */
	@Override
	public MsgEmity findByKey(SimpleUser simpleUser) {
		if (null == simpleUser) {
			return new MsgEmity(false, "条件不能为空！", 91);
		}
		
		return simpleUserDao.findByKey(simpleUser);
	}
	
	/**
	 * 重置密码
	 * @param iId
	 * @return
	 */
	@Override
	public MsgEmity resetPass(Integer iId) {
		if (null == iId || iId.intValue() < 1) {
			return new MsgEmity(false, "用户名不能为空！", 90);
		}
		
		MsgEmity me = simpleUserDao.findById(iId);
		if (!me.isSuccess() || null == me.getData()) {
			return new MsgEmity(false, "没有查找到用户！", 91);
		}
		
		SimpleUser simpleUser = (SimpleUser)me.getData();

		String sOldPass = simpleUser.getsPass();
		String sNewPass = this.getDefaultPass();
		me = simpleUserDao.editPass(iId, sOldPass, sNewPass);
		
		if (!me.isSuccess()) {
			return new MsgEmity(false, "重置密码失败，请稍后重试！", 92);
		}
		
		if (me.getData().equals("91")) {
			return new MsgEmity(false, "重置密码失败，请稍后重试！", 93);
		}
		
		return new MsgEmity(true, "重置密码成功！", me.getData());	
	}

	/**
	 * 判断用户是否存在,存在返回true
	 * @param sName
	 * @param sPass
	 * @return
	 */
	@Override
	public boolean checkUser(String sName, String sPass) {
		if (null == sName || "".equals(sName.trim()) || null == sPass || "".equals(sPass.trim())) {
			return false;
		}
		
		SimpleUser simpleUserData = new SimpleUser();
		simpleUserData.setsName(sName.trim());
		simpleUserData.setsPass(MD5Util.md5(sPass.trim()));
		if (0 == simpleUserDao.findCountByKey(simpleUserData)) {
			return false;
		}
		
		return true;
	}
	
	/**
	 * 取默认用户密码
	 * @return
	 */
	private String getDefaultPass() {
		return MD5Util.md5("123456");
	}
	
}
